package com.tnl.app.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tnl.app.dao.DomainDAO;
import com.tnl.app.dao.EmployeeDAO;
import com.tnl.app.dao.PersonDAO;
import com.tnl.app.entity.Domain;
import com.tnl.app.entity.Employee;
import com.tnl.app.entity.EmployeePerson;
import com.tnl.app.entity.JobDesc;
import com.tnl.app.entity.Person;
import com.tnl.app.service.IEmployeeService;

@Service
@Transactional
public class EmployeeService implements IEmployeeService {
	private static Logger logger = LoggerFactory
			.getLogger(EmployeeService.class);

	@Autowired
	private EmployeeDAO empDAO;
	@Autowired
	private DomainDAO domainDAO;

	@Autowired
	private PersonDAO personDAO;
	
	@Override
	public void updateEmployeeProfile(Employee emp,Person person) {
		personDAO.updateProfile(person);
		empDAO.updateDomain(emp);
	}


	@Override
	public Employee getEmpByPhone(String phone,String openid) {
		return empDAO.getEmpByPhone(phone,openid);
	}

	@Override
	public void saveSecureCode(Employee emp) {

		Person person = personDAO.getByPhone(emp.getPhone());
		int personId = -1;
		if (person == null) {
			int id = personDAO.save(emp);
			personId = id;
			logger.debug("saved id is " + id);
		} else {

			int id = personDAO.update(emp);
			personId = person.getId();
			logger.debug("update person id is " + id);
		}
		emp.setPersonId(personId);
		empDAO.saveSecureCode(emp);
	}


	@Override
	public int updateBaseProfile( Person person) {
		// TODO Auto-generated method stub
		int flag=personDAO.updateBaseProfile(person);
		return flag;
	}


	@Override
	public int SavePersonBaseProfile(Person person) {
		int flag=personDAO.save(person);
		return flag;
	}
	
	@Override
	public void saveResumeDomain(Employee emp,Person person){
		empDAO.updateDomain(emp);
		personDAO.updateDomain(person);
	}
	
	@Override
	public void saveResumeCreateDomain(Employee emp,Person person){
		empDAO.saveDomain(emp);
		personDAO.updateDomain(person);
	}
	
	@Override
	public int saveResumeNowPosi(Person person){
		int flag=personDAO.saveResumeNowPosi(person);
		return flag;
	}
	
	@Override
	public int saveResumeDreamPosi(Employee emp){
		int flag=empDAO.updateDreamPosi(emp);
		return flag;
	}
	public Employee getEmpByPhone(String phone){
		return empDAO.getEmpByPhone(phone);
	}
	
	public Employee getByEmpId(int employeeid){
		return empDAO.getByEmpId(employeeid);
	}
	  
	public Employee getEmpByUnionid(String unionid){
		return empDAO.getEmpByUnionid(unionid);
	}
	
	public Person getById(int personId){
		return personDAO.getById(personId);
	}
	
	 public long bindedPhone(Employee emp){
		 return empDAO.bindedPhone(emp);
	 }
	
	public List<EmployeePerson> searchEmployeesByJob(JobDesc job,Integer personid){
		return empDAO.searchEmployeesByJob(job, personid);
	}
	
	//自动配置的三种方式
	/**
	 * 仅选择目前所在的细分领域
	 * @param job
	 * @param personid
	 * @return
	 */
	public List<EmployeePerson> searchEmployeesByJob1(JobDesc job,Integer personid){
		return empDAO.searchEmployeesByJob1(job, personid);
	}
	
	/**
	 * 选择现领域及跨领域
	 * @param job
	 * @param personid
	 * @return
	 */
	public List<EmployeePerson> searchEmployeesByJob2(JobDesc job,Integer personid){
		return empDAO.searchEmployeesByJob2(job, personid);
	}
	
	/**
	 * 跨医疗/医药领域
	 * @param job
	 * @param personid
	 * @return
	 */
	public List<EmployeePerson> searchEmployeesByJob3(JobDesc job,Integer personid){
		return empDAO.searchEmployeesByJob3(job, personid);
	}


	@Override
	public EmployeePerson save(EmployeePerson emp) {
		System.out.println("personId:"+emp.getPersonId());
		if(emp.getPersonId()==0) {
			int id = personDAO.savePerson(emp);
			emp.setPersonId(id);
			System.out.println(id);
		} else {
			int row = personDAO.updatePerson(emp);
			System.out.println(row);
		}
		System.out.println("empid:"+emp.getEmpid());
		if(emp.getEmpid()==0) {
			int id = empDAO.save(emp);
			emp.setEmpid(id);
			System.out.println(id);
		} else {
			int row = empDAO.update(emp);
			System.out.println(row);
		}
		return emp;
	}


	@Override
	public EmployeePerson getEmpPersonByUnionid(String unionid) {
		EmployeePerson emp = empDAO.getEmpPersonByUnionid(unionid);
		if(emp!=null) {
			if (emp.getMajorDomainId1()!=0) {
				Domain d1 = domainDAO.getById(emp.getMajorDomainId1());
				emp.setMajorDomain1(d1==null?"":d1.getName());
			}
			if (emp.getMajorDomainId2()!=0) {
				Domain d2 = domainDAO.getById(emp.getMajorDomainId2());
				emp.setMajorDomain2(d2==null?"":d2.getName());
			}
			if (emp.getMajorDomainId3()!=0) {
				Domain d3 = domainDAO.getById(emp.getMajorDomainId3());
				emp.setMajorDomain3(d3==null?"":d3.getName());
			}
		}
		return emp;
	}
	
}
